package com.easystudy.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.easystudy.iot.error.ReturnValue;
import com.easystudy.iot.model.BoxInfo;
import com.easystudy.iot.model.RecordInfo;
import com.easystudy.iot.service.IotService;

import lombok.extern.slf4j.Slf4j;

/**
 * @文件名称: AppStartupListener.java
 * @功能描述: 系统初始化监听器
 * @版权信息： www.dondown.com
 * @编写作者： lixx2048@163.com
 * @开发日期： 2020年4月24日
 * @历史版本： V1.0
 */
@Slf4j
@WebListener
public class AppStartupListener implements ServletContextListener {
	@Autowired
	private IotService iotService;
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		log.info("服务正在初始化...");
		startAiBoxService();
		log.info("服务完成初始化！");
	}

	private void startAiBoxService() {
		iotService.register(new com.easystudy.iot.call.Callback() {
			@Override
			public ReturnValue<String> onConnect(BoxInfo boxInfo) {
				System.out.println("盒子上线：" + boxInfo.getId());
				System.out.println("盒子IP：" + boxInfo.getIp());
				return new ReturnValue<String>();
			}
			
			@Override
			public ReturnValue<String> onDisConnect(String boxId) {
				System.out.println("盒子下线：" + boxId);
				return new ReturnValue<String>();
			}

			@Override
			public ReturnValue<String> onAlarm(String boxId, com.easystudy.iot.model.AlarmInfo alarm) {
				System.out.println("盒子:" + boxId);
				System.out.println("报警:" + alarm.getId());
				return new ReturnValue<String>();
			}

			@Override
			public ReturnValue<String> onRecord(String boxId, RecordInfo record) {
				System.out.println("盒子:" + boxId);
				System.out.println("录像:" + record);
				return new ReturnValue<String>();
			}

			@Override
			public ReturnValue<String> onHeart(String boxId, com.easystudy.iot.model.HeartInfo heartInfo) {
				System.out.println("盒子:" + boxId);
				System.out.println("心跳:" + heartInfo);
				return new ReturnValue<String>();
			}

			@Override
			public ReturnValue<String> onResult(String boxId, String cmdId, Integer state, 
					String desc, String json, MultipartFile file) {
				System.out.println("盒子:" + boxId);
				System.out.println("响应结果:" + cmdId + ":" + state + ":" + desc);
				System.out.println("响应参数:" + json);
				System.out.println("响应文件:" + file);
				return new ReturnValue<String>();
			}
		});
	}
}
